<template>
    <div class="app-container">
        <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="订单编号" prop="outTradeNo">
                <el-input v-model="queryParams.outTradeNo" placeholder="订单编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="订单状态" prop="serviceStatus">
                <el-select v-model="queryParams.serviceStatus" placeholder="请选择订单状态">
                    <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="退款时间" prop="refundTime">
                <el-date-picker clearable v-model="queryParams.refundTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择付款时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                    v-hasPermi="['ski:order:list']">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['ski:sale:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="orderList">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="用户名称" align="center" prop="memberName" show-overflow-tooltip />
            <el-table-column label="退款编号" align="center" prop="outRefundNo" show-overflow-tooltip />
            <el-table-column label="订单编号" align="center" prop="outTradeNo" show-overflow-tooltip />
            <el-table-column label="申请退款金额" align="center" prop="applyRefundPrice" show-overflow-tooltip />
            <el-table-column label="申请时间" align="center" prop="createTime" show-overflow-tooltip />
            <!-- <el-table-column label="申请原因" align="center" prop="reason" show-overflow-tooltip />
            <el-table-column label="问题描述" align="center" prop="problemDesc" show-overflow-tooltip /> -->
            <el-table-column label="商家备注" align="center" prop="auditRemark" show-overflow-tooltip /> -->

            <el-table-column label="售后状态" align="center" width="120">
                <template slot-scope="scope">
                    <el-tag size="small" v-if="scope.row.serviceStatus == 'paid'">已支付</el-tag>
                    <el-tag type="info" size="small" v-if="scope.row.serviceStatus == 'canceled'">已取消</el-tag>
                    <el-tag type="success" size="small" v-if="scope.row.serviceStatus == 'completed'">完成退款</el-tag>
                    <el-tag type="warning" size="small" v-if="scope.row.serviceStatus == 'applyRefund'">申请中</el-tag>
                    <el-tag type="danger" size="small" v-if="scope.row.serviceStatus == 'refuseRefund'">拒接退款</el-tag>
                    <el-tag type="success" size="small" v-if="scope.row.serviceStatus == 'agreeRefund'">同意退款</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="150">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
                        v-hasPermi="['ski:sale:query']">查看</el-button>

                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['ski:sale:edit']" v-if="scope.row.serviceStatus != 'applyRefund'">删除</el-button>

                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleTuikuan(scope.row)"
                        v-hasPermi="['ski:sale:edit']" v-if="scope.row.serviceStatus == 'applyRefund'">退款</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 详情对话框 -->
        <el-dialog :title="title" width="700px" :visible.sync="dialogVisible" append-to-body>
            <el-form :model="refundForm" ref="refundForm" label-width="70px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="用户名称">
                            <el-input type="text" v-model="formLabelAlign.skiAfterSale.memberName" />
                        </el-form-item>
                        <el-form-item label="退款单号">
                            <el-input type="text" v-model="formLabelAlign.skiAfterSale.outRefundNo" />
                        </el-form-item>
                        <el-form-item label="退款金额">
                            <el-input type="text" v-model="formLabelAlign.skiAfterSale.applyRefundPrice" />
                        </el-form-item>
                        <el-form-item label="商家名称">
                            <el-input type="text" v-model="formLabelAlign.orderDetailInfoVo.orderDetail.snowFieldName" />
                        </el-form-item>
                        <el-form-item label="是否同意">
                            <el-tag type="success"
                                v-if="formLabelAlign.skiAfterSale.serviceStatus == 'agreeRefund'">同意</el-tag>
                            <el-tag type="warning"
                                v-if="formLabelAlign.skiAfterSale.serviceStatus == 'refuseRefund'">拒绝</el-tag>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="雪场名称">
                            <el-input type="text" v-model="formLabelAlign.orderDetailInfoVo.orderDetail.snowFieldName" />
                        </el-form-item>
                        <el-form-item label="套餐名称">
                            <el-input type="text" v-model="formLabelAlign.orderDetailInfoVo.orderDetail.setMealName" />
                        </el-form-item>
                        <el-form-item label="套餐详情">
                            <el-input type="text" v-model="formLabelAlign.orderDetailInfoVo.orderDetail.setMealDesc" />
                        </el-form-item>
                        <el-form-item label="套餐金额">
                            <el-input type="text" v-model="formLabelAlign.orderDetailInfoVo.orderDetail.setMealPrice" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="备注信息" prop="auditRemark">
                    {{ formLabelAlign.skiAfterSale.auditRemark }}
                </el-form-item>
            </el-form>
        </el-dialog>

        <!-- 退款对话框 -->
        <el-dialog :title="title" width="500px" :visible.sync="open" append-to-body>
            <el-form :model="form" :rules="rules" ref="form" label-width="80px">
                <el-form-item label="是否同意" prop="serviceStatus">
                    <el-radio v-model="form.serviceStatus" label="agreeRefund">同意</el-radio>
                    <el-radio v-model="form.serviceStatus" label="refuseRefund">拒绝</el-radio>
                </el-form-item>
                <el-form-item label="退款金额" prop="actualRefundPrice" v-if="form.serviceStatus === 'agreeRefund'">
                    <el-input-number v-model="form.actualRefundPrice" controls-position="right" :min="0"
                        :max="9999"></el-input-number>
                </el-form-item>
                <el-form-item label="备注信息" prop="auditRemark">
                    <el-input type="textarea" v-model="form.auditRemark"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="title != '雪场信息详情'">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import { listRefund, searchRefund, dealRefund, delOrder } from "@/api/business/refund"
import { listMeal } from "@/api/business/meal";
export default {
    data() {
        return {
            // 显示搜索条件
            showSearch: true,
            // 非多个禁用
            multiple: true,
            // 遮罩层
            loading: true,
            total: 0,
            title: '',
            // 详情抽屉
            statuisInfo: '', // 商品状态
            auditRemark: '', // 备注信息
            dialogVisible: false,
            open: false,
            refundForm: {
                afterSaleId: '',
                actualRefundPrice: null,
                serviceStatus: 'agreeRefund',
                auditRemark: '',
            },
            formLabelAlign: {
                orderDetailInfoVo: {
                    order: {},
                    orderDetail: {}
                },
                skiAfterSale: {}
            },
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                outTradeNo: null,
                refundTime: null,
                serviceStatus: null,
            },
            orderList: [],
            statusList: [
                { value: 'paid', label: '已支付' },
                { value: 'canceled', label: '已取消' },
                { value: 'completed', label: '完成' },
                { value: 'applyRefund', label: '申请退款' },
                { value: 'refuseRefund', label: '拒接退款' },
                { value: 'agreeRefund', label: '同意退款' },
            ],
            setMealList: [],
            // 表单参数
            form: {
                serviceStatus: '', //  售后状态
                actualRefundPrice: '', // 实际退款金额
                auditRemark: '', // 商家备注
                afterSaleId: '', //售后订单id
            },
            // 表单校验
            rules: {
                actualRefundPrice: [{ required: true, message: '请输入退款金额', trigger: 'blur' }],
                auditRemark: [{ required: true, message: '请输入备注信息', trigger: 'blur' },]
            },
        };
    },
    created() {
        this.getList()
    },
    methods: {
        // 查询套餐
        getMeal() {
            listMeal({ pageNum: 1, pageSize: 10, },).then(response => {
                this.setMealList = response.rows;
            });
        },
        getList() {
            listRefund(this.queryParams).then(response => {
                console.log(response, "售后列表");
                this.orderList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 详情
        handleDetail(row) {
            this.dialogVisible = true
            // this.drawer = true
            searchRefund(row.id).then(response => {
                console.log('response--------: ', response)
                this.formLabelAlign = response.data
                this.refundForm.afterSaleId = response.data.skiAfterSale.id
                this.statuisInfo = response.data.skiAfterSale.serviceStatus
                this.auditRemark = response.data.skiAfterSale.auditRemark
                this.title = "订单信息详情";
            });
        },

        // 删除
        handleDelete(row) {
            // console.log('row--------: ', row)
            const orderIds = row.id || this.ids;
            this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function () {
                return delOrder(orderIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },

        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },

        handleClose() {
            this.dialogVisible = false
            this.$refs.refundForm.resetFields();
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/system/afterSale/export', {
                ...this.queryParams
            }, `order_${new Date().getTime()}.xlsx`)
        },

        // 退款操作
        handleTuikuan(row) {
            console.log('row----------: ', row)
            this.reset()
            this.open = true
            this.title = '退款操作'
            this.form.afterSaleId = row.id
        },

        // 表单重置
        reset() {
            this.form = {
                serviceStatus: 'agreeRefund', //  售后状态
                actualRefundPrice: '', // 实际退款金额
                auditRemark: '', // 商家备注
                afterSaleId: '', //售后订单id
            }
            this.resetForm("form");
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        /** 提交按钮 */
        submitForm() {
            console.log('this.form-----------: ', this.form)
            if (this.form.serviceStatus === "refuseRefund") {
                delete (this.form.actualRefundPrice)
            }
            this.$refs["form"].validate(valid => {
                if (valid) {
                    dealRefund(this.form).then(response => {
                        console.log('dealRefund--------: ', response)
                        this.$modal.msgSuccess("操作成功");
                        this.open = false;
                        this.getList();
                    });
                }
            })
        }
    }
};
</script>
